<?php

require_once 'net.php';

class SMTPResponse
{
    public $_M_code;
    public $_M_mnemo;
    public $_M_data;
    public $_M_response;

    function __construct($response)
    {
        $this->_M_response = "{$response}";
        $this->_M_code  = 0;
        $this->_M_mnemo = '';
        $this->_M_data  = '';

        $this->parse();
    }

    function code()
    {
        return $this->_M_code;
    }

    function mnemo()
    {
        return $this->_M_mnemo;
    }

    function data()
    {
        return $this->_M_data;
    }

    function response()
    {
        return $this->_M_response;
    }

    function parse()
    {
        $response = ltrim($this->_M_response);
        if (strlen($response) >= 3) {
            $code = substr($response, 0, 3);
            if (is_numeric($code)) {
                $this->_M_code = intval($code);
            } else {
                throw new Exception("Can't parse SMPT response: '{$this->_M_response}'");
            }
            $response = substr($response, 3);
        } else {
            throw new Exception("Can't parse SMPT response: '{$this->_M_response}'");
        }
        if (($pos = strpos($response, ' ')) !== false) {
            $this->_M_mnemo = substr($response, 0, $pos);
            $this->_M_data = rtrim(substr($response, $pos+1));
        } else {
            $this->_M_mnemo = rtrim($response);
        }
    }

    function toHTML()
    {
        $html = '';
        $html .= '<span style="font-weight: bold;">' . $this->_M_code . '</span>';
        $html .= '<span style="text-decoration: underline;">' . nl2br(htmlentities($this->_M_mnemo)) . '</span>';
        $html .= ' <span style="font-style: italic;">' . nl2br(htmlentities($this->_M_data)) . '</span>';
        return $html;
    }

}

function SMTPResponse($response)
{
    return new SMTPResponse($response);
}

class SMTPClient extends NetClient
{
    protected $_M_crlf;
    protected $_M_status;

    function __construct($params)
    {
        parent::__construct($params);
        $this->_M_status = false;
        $this->_M_crlf = "\r\n";
    }

    function response()
    {
        return new SMTPResponse(parent::response());
    }

    function post($msg)
    {
        return parent::post($msg.$this->_M_crlf);
    }

    function helo($data)
    {
        $this->request("HELO {$data}");
    }

    function ehlo($data)
    {
        $this->request("EHLO {$data}");
    }

    function mail($data)
    {
        $this->request("MAIL FROM:{$data}");
    }

    function rcpt($data)
    {
        $this->request("RCPT TO:{$data}");
    }

    function data()
    {
        $this->request("DATA");
        $this->_M_status = false;
    }

    function msg_line($data)
    {
        $this->post($data);
    }

    function end_data()
    {
        $res = $this->request(".");
        if ($this->has_response() && $this->response()->code() < 400) {
            $this->_M_status = true;
        } else {
            $this->_M_status = false;
        }
    }

    function rset()
    {
        $this->request("RSET");
    }

    function quit()
    {
        $this->request("QUIT");
    }

    function status()
    {
        return $this->_M_status;
    }

}



?>